﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<style type="text/css">
    div#rMenu
    {
        position: absolute;
        visibility: hidden;
        background-color: #555;
        text-align: left;
        padding: 2px;
    }
    div#rMenu ul li
    {
        margin: 1px 0;
        padding: 0 5px;
        cursor: pointer;
        list-style: none outside none;
        background-color: #DFDFDF;
    }
</style>

<script type="text/javascript">
    function onFiletreeCheck(event, treeId, treeNode) {
        alert(treeNode.id + ", " + treeNode.name + "," + treeNode.checked);
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getCheckedNodes(true);

        var temp = "";
        for (i = 0; i < nodes.length; i++) {
            temp = temp + nodes[i].id + ",";
        }
        $("#selectarr").attr("value", temp);
    };

    function OnFiletreeClick(event, treeId, treeNode) {
        alert(treeNode.tId + ", " + treeNode.name + " , " + treeNode.id + " , " + treeNode.tag);
    };


    function OnFiletreeRightClick(event, treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
            zTree.cancelSelectedNode();
            showRMenu("root", event.clientX, event.clientY);
        } else if (treeNode && !treeNode.noR) {
            zTree.selectNode(treeNode);
            showRMenu("node", event.clientX, event.clientY);
        }
    }
    function showRMenu(type, x, y) {
        var rMenu = $("#rMenu");
        $("#rMenu ul").show();
        if (type == "root") {
            $("#m_del").hide();
            $("#m_check").hide();
            $("#m_unCheck").hide();
        } else {
            $("#m_del").show();
            $("#m_check").show();
            $("#m_unCheck").show();
        }
        y = y + 330;
        rMenu.css({ "top": y + "px", "left": x + "px", "visibility": "visible" });
        $("body").bind("mousedown", onBodyMouseDown);
    }
    function hideRMenu() {
        var rMenu = $("#rMenu");
        if (rMenu) rMenu.css({ "visibility": "hidden" });
        $("body").unbind("mousedown", onBodyMouseDown);
    }
    function onBodyMouseDown(event) {
        var rMenu = $("#rMenu");
        if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
            rMenu.css({ "visibility": "hidden" });
        }
    }
    var addCount = 1;
    function addTreeNode() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        hideRMenu();
        var filefullname = zTree.getSelectedNodes()[0].tag + "\\" + "增加" + addCount;
        zTree.addNodes(zTree.getSelectedNodes()[0], [{ id: 0, name: "增加" + addCount, nocheck: true, tag: filefullname, children: [{}]}]);
        
        $.ajax({
            type: "POST",
            url: "/File/DoFileAdd",
            data: { "path": filefullname },
            success: function(data) {
                alert(data);
                addCount++;
            }
        })
    }
    
    function removeTreeNode() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        hideRMenu();
        var nodes = zTree.getSelectedNodes();
        if (nodes && nodes.length > 0) {
            if (nodes[0].children && nodes[0].children.length > 0) {
                var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
                if (confirm(msg) == true) {
                    zTree.removeNode(nodes[0]);
                }
            } else {
                zTree.removeNode(nodes[0]);
            }
        }
    }
    
    function checkTreeNode(checked) {
        var nodes = zTree.getSelectedNodes();
        if (nodes && nodes.length > 0) {
            zTree.checkNode(nodes[0], checked, true);
        }
        hideRMenu();
    }


    function resetTree() {
        hideRMenu();
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    } 
</script>

<div id="filetree">
    <div id="hide">
        <input type="hidden" id="selectarr" />
    </div>
    <div id="div_tree" class="zTreeDemoBackground left">
        <ul id="treeDemo" class="ztree">
        </ul>
    </div>
    <div id="rMenu" style="position: absolute; visibility: hidden; top: 158px; left: 390px;">
        <li>
            <ul onclick="addTreeNode();" id="m_add">
                <li>增加</li></ul>
            <ul onclick="removeTreeNode();" id="m_del">
                <li>删除</li></ul>
            <ul onclick="reloadTree();" id="m_reset">
                <li>刷新</li></ul>
        </li>
    </div>
</div>
